<?php
Cinnamon::loadClass('DatabaseCollection');
Cinnamon::loadClass('DatabaseFilter');
Cinnamon::loadClass('DatabasePeriodFilter');

/**
 * Filter Manager class
 */
class FilterManager {
	/**
	 * Array of enabled filters
	 */
	protected $filters;
	/**
	 * Hashed array of tables that have one or more filters
	 */
	protected $tables;
	
	/**
	 * Default constructor
	 */
	public function __construct() {
		// empty tables
		$this->filters = array();
		$this->tables = array();
		
		$db = Cinnamon::registry('db');
		
		// default filters - that can not be disabled
		// loaded manually
		
		// 1. Site
		$filter = new DatabaseFilter(
			'Site', $db->requests, 'site_id', 
			$db->sites('site_id AS value, site_name AS label')->order('site_name')
		);
		$filter->any = false;
		$this->addFilter($filter);
		unset($filter);
		
		// 2. Date
		$this->addFilter( new DatabasePeriodFilter('Period') );
		/*$dp = new SQLDataProvider(false);		// false == do not apply filters
		$dp->columns('site_id AS value, site_name AS label')->from($db->sites)->order('site_name');
		$filter = new Filter('site', $db->requests, 'site_id', 'selection.all', false);
		$filter->setDataProvider($dp);
		$this->filters['site'] = $filter;
		$this->tables[$db->requests] = array();
		array_push($this->tables[$db->requests], $filter);
		*/
		
		// 2. Date
		/*$filter = new DateFilter('period', $db->requests, 'request_datetime', 'selection.all', true);
		$this->filters['period'] = $filter;
		array_push($this->tables[$db->requests], $filter);*/
	}
	
	/**
	 * Adds the specified filter
	 * @param IFilter $filter the filter instance to be added
	 */
	public function addFilter($filter) {
		array_push($this->filters, $filter);
	}
	
	/**
	 * Removes the specified filter
	 * @param IFilter $filter the filter instance to be added
	 */
	public function removeFilter($filter) {
	}

	public function render() {
		require_once CSEPATH .'/view/filters.php';
	}
	
	public function getAllInstances() {
		return $this->filters;
	}
	
	public function filter($collection) {
		foreach ( $this->filters as $filter ) 
			$filter->filter($collection);
	} 
}

?>